# Makefile for interceptor library on macOS
# Author: sunboy
# Date: 2025-10-13

# 编译器设置
CC = clang
CFLAGS = -Wall -Wextra -fPIC -O2

# 库名称和版本
LIB_NAME = interceptor
LIB_VERSION = 1.0.0
DYLIB_NAME = lib$(LIB_NAME).dylib
DYLIB_VERSIONED = lib$(LIB_NAME).$(LIB_VERSION).dylib

# 目录设置
SRC_DIR = .
BUILD_DIR = ../
INSTALL_DIR = /usr/local/lib

# 源文件和目标文件
SOURCES = interceptor.c
OBJECTS = $(SOURCES:.c=.o)
HEADERS = interceptor.h uiohook.h

# 链接库设置
# 假设 uiohook 库在父目录
LDFLAGS = -L$(BUILD_DIR) -dynamiclib
LIBS = -luiohook.1.2.0

# macOS 特定设置
MACOS_FLAGS = -install_name @rpath/$(DYLIB_NAME)
MACOS_COMPAT = -compatibility_version 1.0 -current_version $(LIB_VERSION)

# 默认目标
.PHONY: all
all: $(DYLIB_NAME)

# 编译目标文件
%.o: %.c $(HEADERS)
	@echo "Compiling $<..."
	$(CC) $(CFLAGS) -c $< -o $@

# 生成动态链接库
$(DYLIB_NAME): $(OBJECTS)
	@echo "Linking dynamic library..."
	$(CC) $(LDFLAGS) $(MACOS_FLAGS) $(MACOS_COMPAT) \
		-o $(BUILD_DIR)$(DYLIB_VERSIONED) $(OBJECTS) $(LIBS)
	@echo "Creating symlink..."
	cd $(BUILD_DIR) && ln -sf $(DYLIB_VERSIONED) $(DYLIB_NAME)
	@echo "Build successful: $(BUILD_DIR)$(DYLIB_NAME)"

# 安装到系统目录（需要 sudo）
.PHONY: install
install: $(DYLIB_NAME)
	@echo "Installing library to $(INSTALL_DIR)..."
	@mkdir -p $(INSTALL_DIR)
	@cp $(BUILD_DIR)$(DYLIB_VERSIONED) $(INSTALL_DIR)/
	@cd $(INSTALL_DIR) && ln -sf $(DYLIB_VERSIONED) $(DYLIB_NAME)
	@echo "Installation complete"

# 清理编译产物
.PHONY: clean
clean:
	@echo "Cleaning build artifacts..."
	@rm -f $(OBJECTS)
	@rm -f $(BUILD_DIR)$(DYLIB_NAME)
	@rm -f $(BUILD_DIR)$(DYLIB_VERSIONED)
	@echo "Clean complete"

# 深度清理（包括备份文件）
.PHONY: distclean
distclean: clean
	@rm -f *~
	@rm -f *.bak

# 显示编译信息
.PHONY: info
info:
	@echo "=== Build Information ==="
	@echo "Compiler: $(CC)"
	@echo "Source files: $(SOURCES)"
	@echo "Object files: $(OBJECTS)"
	@echo "Library name: $(DYLIB_NAME)"
	@echo "Library version: $(LIB_VERSION)"
	@echo "Build directory: $(BUILD_DIR)"
	@echo "========================="

# 检查依赖
.PHONY: check-deps
check-deps:
	@echo "Checking dependencies..."
	@if [ ! -f "$(BUILD_DIR)libuiohook.1.2.0.dylib" ]; then \
		echo "Error: libuiohook.1.2.0.dylib not found in $(BUILD_DIR)"; \
		exit 1; \
	fi
	@echo "Dependencies OK"

# 构建并运行示例
.PHONY: example
example: $(DYLIB_NAME)
	@echo "Building example..."
	$(CC) $(CFLAGS) -L$(BUILD_DIR) -linterceptor -luiohook.1.2.0 \
		example.c -o $(BUILD_DIR)example
	@echo "Running example..."
	cd $(BUILD_DIR) && ./example

# 调试版本编译
.PHONY: debug
debug: CFLAGS += -g -DDEBUG -O0
debug: clean all
	@echo "Debug build complete"

# 帮助信息
.PHONY: help
help:
	@echo "Available targets:"
	@echo "  all        - Build the dynamic library (default)"
	@echo "  clean      - Remove build artifacts"
	@echo "  distclean  - Remove all generated files"
	@echo "  install    - Install library to system (requires sudo)"
	@echo "  info       - Show build configuration"
	@echo "  check-deps - Check if dependencies exist"
	@echo "  example    - Build and run example program"
	@echo "  debug      - Build debug version"
	@echo "  help       - Show this help message"
